//
//  UIView+UIViewUtils.m
//  HZProgress
//
//  Created by 王会洲 on 16/4/8.
//  Copyright © 2016年 王会洲. All rights reserved.
//

#import "UIView+UIViewUtils.h"
#import "MSHudLoadingView.h"
#import "MSHUDCircleProgressView.h"
#import "MSHUDAtmosphereView.h"
#import "OPBHUDLoadingCircleView.h"
#import "MSScaleLoadingView.h"


typedef enum : NSUInteger {
    MSHUDStyleDefault, //头部有圆圈显示
    MSHUDStyleLabel, //只显示文字内容
    MSHUDStyleNormal ,    //常用样式
} MSShowHUDViewStyle;


#define kMSHUDTOPWINDOW [self getCurrentWindowView]

@implementation UIView (UIViewUtils)

/// 显示文字提示（自动隐藏）
/// @param indiTitle 提醒文字
- (void)showHUDIndicatorAtCenter:(NSString *)indiTitle {
    MSProgressHUD *hud = [self getHUDIndicatorViewAtCenter];
    if (hud) {
        [hud hideAnimated:YES];
    }
    hud = [self createHUDIndicatorViewAtCenter:indiTitle HUDCase:MSHUDStyleLabel];
    [hud showAnimated:YES];
    [self hide:YES afterDelay:1.25];
}


/// 展示提示toast，不自动消失
/// @param title title description
- (void)showHUDAtCenter:(NSString *)title {
    MSProgressHUD *hud = [self getHUDIndicatorViewAtCenter];
    if (hud) {
        [hud hideAnimated:YES];
    }
    hud = [self createHUDIndicatorViewAtCenter:title HUDCase:MSHUDStyleLabel];
    [hud showAnimated:YES];
}




/// 只显示文字提示（自动隐藏）
/// @param indiTitle 提示文案
/// @param y 偏移量
- (void)showHUDIndicatorViewAtCenter:(NSString *)indiTitle yOffset:(CGFloat)y {
    MSProgressHUD *hud = [self getHUDIndicatorViewAtCenter];
    if (hud) {
        // 如果有已经展示的隐藏
        [hud hideAnimated:YES];
    }
    hud = [self createHUDIndicatorViewAtCenter:indiTitle icon:nil yOffset:y MPBCase:MSHUDStyleLabel];
    [hud showAnimated:YES];
    [self hide:YES afterDelay:1.25];
}


/**延迟加载控制*/
- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay {
    [self performSelector:@selector(hideDelayed:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay];
}

- (void)hideDelayed:(NSNumber *)animated {
    [self hideHUDIndicatorAtCenter];
}



/// 创建HUD对象
/// @param indiTitle indiTitle 标题
/// @param ShowHUDStyle 显示样式
- (MSProgressHUD *)createHUDIndicatorViewAtCenter:(NSString *)indiTitle
                                          HUDCase:(MSShowHUDViewStyle)ShowHUDStyle {
    return [self createHUDIndicatorViewAtCenter:indiTitle icon:nil yOffset:0 MPBCase:ShowHUDStyle];
}


- (void)showHUDIndicatorAtCenter {
    MSProgressHUD *oldhud = [self getHUDIndicatorViewAtCenter];
    if (oldhud) {
        // 如果有已经展示的隐藏
        [oldhud hideAnimated:NO];
    }
    
    MSProgressHUD * hud = [[MSProgressHUD alloc] initWithView:self];
    hud.mode = MSProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    MSHudLoadingView *customView = [[MSHudLoadingView alloc] init];
    hud.customView = customView;
    hud.bezelView.style = MSProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    hud.userInteractionEnabled = YES;
    [self addSubview:hud];
    hud.tag = hudViewTag;
    [hud showAnimated:YES];
}

- (void)showScaleHUDIndicatorAtCenter {
    MSProgressHUD *oldhud = [self getHUDIndicatorViewAtCenter];
    if (oldhud) {
        // 如果有已经展示的隐藏
        [oldhud hideAnimated:NO];
    }
    
    MSProgressHUD * hud = [[MSProgressHUD alloc] initWithView:self];
    hud.mode = MSProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    MSScaleLoadingView *customView = [[MSScaleLoadingView alloc] init];
    hud.customView = customView;
    hud.bezelView.style = MSProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    hud.userInteractionEnabled = YES;
    [self addSubview:hud];
    hud.tag = hudViewTag;
    [hud showAnimated:YES];
}


- (void)showHUDCircleAtCenter {
    MSProgressHUD *oldhud = [self getHUDIndicatorViewAtCenter];
    if (oldhud) {
        // 如果有已经展示的隐藏
        [oldhud hideAnimated:NO];
    }
    
    MSProgressHUD * hud = [[MSProgressHUD alloc] initWithView:self];
    hud.mode = MSProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    MSHUDCircleProgressView *circleProgress = [[MSHUDCircleProgressView alloc] initWithDuration:15];
    hud.customView = circleProgress;
    hud.bezelView.style = MSProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    hud.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    hud.userInteractionEnabled = YES;
    [self addSubview:hud];
    hud.tag = hudViewTag;
    [hud showAnimated:YES];
}




-(void)showHUDCircleOnWebView {
    MSProgressHUD *oldhud = [self getHUDIndicatorViewAtCenter];
    if (oldhud) {
        // 如果有已经展示的隐藏
        [oldhud hideAnimated:NO];
    }

    MSProgressHUD * hud = [[MSProgressHUD alloc] initWithView:self];
    hud.mode = MSProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    OPBHUDLoadingCircleView *circleProgress = [[OPBHUDLoadingCircleView alloc] init];
    hud.customView = circleProgress;
    hud.bezelView.style = MSProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    [hud setOffset:CGPointMake(0, 44)];
    hud.userInteractionEnabled = YES;
    [self addSubview:hud];
    hud.tag = hudViewTag;
    [hud showAnimated:YES];
}


- (void)showHUDSuccessAtCenter:(void(^)(void))complated {
    MSProgressHUD *oldhud = [self getHUDIndicatorViewAtCenter];
    if (oldhud) {
        // 如果有已经展示的隐藏
        [oldhud hideAnimated:NO];
    }

    MSProgressHUD * hud = [[MSProgressHUD alloc] initWithView:self];
    hud.mode = MSProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    MSHUDAtmosphereView *circleProgress = [[MSHUDAtmosphereView alloc] init];
    
    __weak typeof(self) ws = self;
    [circleProgress setConfigHandler:^{
        [ws hideHUDIndicatorAtCenter];
        if (complated) {
            complated();
        }
    }];
    hud.customView = circleProgress;
    hud.bezelView.style = MSProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    hud.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    hud.userInteractionEnabled = YES;
    [self addSubview:hud];
    hud.tag = hudViewTag;
    [hud showAnimated:YES];
}



- (void)hideHUDIndicatorAtCenter {
    MSProgressHUD *hud = [self getHUDIndicatorViewAtCenter];
    [hud hideAnimated:YES];
}




/// 创建HUD对象
/// @param indiTitle 标题
/// @param icon 图标
/// @param y y轴偏移量
/// @param ShowHUDStyle 显示样式
- (MSProgressHUD *)createHUDIndicatorViewAtCenter:(NSString *)indiTitle
                                             icon:(NSString *)icon
                                          yOffset:(CGFloat)y
                                          MPBCase:(MSShowHUDViewStyle)ShowHUDStyle {
    MSProgressHUD *hud = [[MSProgressHUD alloc] initWithView:self];
    hud.layer.zPosition = 10;
    hud.offset = CGPointMake(hud.offset.x, y);
    hud.removeFromSuperViewOnHide = YES;
    hud.detailsLabel.text = indiTitle;
    
    switch (ShowHUDStyle) {
        
        case MSHUDStyleLabel:
        {
            hud.mode = MSProgressHUDModeText;
            hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.9];
            hud.detailsLabel.textColor = [UIColor whiteColor];
            hud.detailsLabel.font = [UIFont systemFontOfSize:16];
        }
            break;

        default:
            break;
    }
    [self addSubview:hud];
    hud.tag = hudViewTag;
    return hud;
}


/// 获取HUD对象
- (MSProgressHUD *)getHUDIndicatorViewAtCenter {
    UIView *view = [self viewWithTagNotDeepCounting:hudViewTag];
    if (view != nil && [view isKindOfClass:[MSProgressHUD class]]){
        return (MSProgressHUD *)view;
    } else {
        return nil;
    }
}

- (UIView *)viewWithTagNotDeepCounting:(NSInteger)tag {
    for (UIView *view in self.subviews) {
        if (view.tag == tag) {
            return view;
            break;
        }
    }
    return nil;
}



/**获取当前window*/
- (UIWindow *)getCurrentWindowView {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    return window;
}

@end
